const {decrypto,encrypto} = require("./crypt")
const reg = /^\/api\/admin/;
const {cookieName} = require("../routerConfig");
const {whyi} = require('../server/adminServer');
const { getR} = require("../server/redisServe")
module.exports = async (req,res,next)=>{
    if(reg.test(req.path) && req.path != '/api/admin/login' ){
         let id = null;
         if(req.cookies[cookieName]){
           
           id = decrypto(req.cookies[cookieName]);
           
         }else if(req.headers['admin-token']){
          
           id = decrypto(req.headers['admin-token'])
           
         }
          
         const CResult = await getR(5,id)
         if(CResult === 'admin'){
            req.adminId = id;
            next();
            return;

         }else{

           const result = await  whyi(id);
           if(result){
                req.adminId = id;
                next();
                return
           }else{
              res.status(200).send({
                  code: 404,
                  msg:"token err"
              });
              return;
           }

         }
    }
    next();

}
